home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / struct10.zip / STRUCT.C next >
C/C++ Source or Header  |  1995-01-25  |  10KB  |  333 lines

  1.  
  2. //
  3. // Program:     STRUCT C/C++ code organizator.   
  4. //
  5. // Author:      Ahmet Emre (CompuServe #100335,2771)
  6. //
  7. // Purpose:     If you are C/C++ programmer and suffering from different
  8. //              tab settings of different programmers and editors, this
  9. //              program is what you want. This program erases tab characters
  10. //              and inserts blank spaces to beginning of each line according
  11. //              to nested braces like { and }. Main purpose of this program
  12. //              is to make your sources more readable.              
  13. //
  14. // Copyright:   Copyright 1995, The Hekla Information Society, All Rights Reserved.
  15. //
  16. // Version:     Version 1.0 for DOS.
  17. // 
  18. // Shareware:   This program is ShareWare. There is no charge on using or
  19. //              copying this program.
  20. //
  21. // Distribution:You may distribute this program unmodified as long as all
  22. //              accompanying documentation and notes are also distributed.
  23. //
  24. // Disclaimer:  This program is provided as is with no implied or express
  25. //              warranties.  You should test this class for your particular
  26. //              use on non-critical data before using it in a production
  27. //              system.
  28. //          
  29.  
  30. #include <stdio.h>  
  31. #include <stdlib.h>
  32. #include <ctype.h>
  33. #include <string.h>    
  34. #include <dos.h>
  35.  
  36. #define MAX_LINE_SIZE   500
  37.  
  38. FILE *in,*out;
  39.  
  40. int spaceno=4; // number of spaces to insert (default is 4)
  41. int ob,colptr=0,
  42. araprev, arabra; // 1 if brackets in the middle of line.
  43.  
  44. char inname[100];
  45. char outname[15]="$BJK1903.$$$";
  46.  
  47. char buffer[MAX_LINE_SIZE];     
  48. char outbuffer[MAX_LINE_SIZE];     
  49.  
  50.  
  51. //+ Function : void OutFileWrite(char c)
  52. //+ Purpose  : writes to temporary output file.
  53. //+ Input    : char c  : current processing character.
  54. //+ Output   : none.
  55.  
  56. void OutFileWrite(char c)
  57. {
  58.     char finalbuffer[MAX_LINE_SIZE];
  59.     int cp;
  60.     
  61.     memset(finalbuffer,' ',MAX_LINE_SIZE);  
  62.     
  63.     if (ob>0 && (c=='{' || c=='}'))
  64.     {
  65.         sprintf(finalbuffer+colptr,"%s",outbuffer);
  66.         fprintf(out,"%s\n",finalbuffer);
  67.         memset(outbuffer,'\0',MAX_LINE_SIZE);
  68.     }
  69.     
  70.     if (c=='}') 
  71.     {
  72.         colptr -= spaceno;
  73.         sprintf(outbuffer,"}");    
  74.         if (colptr < 0) colptr=0;
  75.     }
  76.     
  77.     if (arabra) cp=araprev;
  78.     else cp=colptr;
  79.     
  80.     
  81.     if (c=='{') 
  82.     {
  83.         sprintf(outbuffer,"{");  
  84.         cp=colptr;
  85.         colptr += spaceno;
  86.     }
  87.     
  88.     memset(finalbuffer,' ',MAX_LINE_SIZE);  
  89.     
  90.     sprintf(finalbuffer+cp,"%s",outbuffer);
  91.     fprintf(out,"%s\n",finalbuffer);
  92.     
  93. }
  94.  
  95.  
  96. //+ Function : int ccopy(char *from,char *to)
  97. //+ Purpose  : Copies files. Use this instead of escaping to the shell for
  98. //+            the copies.
  99. //+ Input    : char *from : Source file name. NULL terminated.
  100. //+            char *to   : Target file name. NULL terminated.
  101. //+ Output   : Returns : 0 : No error.
  102. //+                      else : various errors.
  103.  
  104. int ccopy(char *from,char *to)
  105. {
  106.     FILE *f,*t;  
  107.     long sz;
  108.     int rc;
  109.     
  110.     f = fopen(from,"rb");
  111.     if (f==NULL) return(11);
  112.     t = fopen(to,"wb");
  113.     if (t==NULL) return(12);
  114.     fseek(f,0L,SEEK_END);
  115.     sz = ftell(f);
  116.     fseek(f,0L,SEEK_SET);
  117.     
  118.     while (sz > 0)
  119.     {
  120.         if (sz > 512)
  121.         rc = 512;
  122.         else    rc = sz;
  123.         sz -= rc;
  124.         if (1 != fread(&buffer,rc,1,f)) return(21);
  125.         if (1 != fwrite(&buffer,rc,1,t)) return(22);
  126.     }
  127.     if (fclose(t)) return(31);
  128.     if (fclose(f)) return(32);
  129.     return(0);
  130. }
  131.  
  132. //+ Function : char *FindPath(char *s)
  133. //+ Purpose  : analyzes a string and finds path by excluding filename.
  134. //+ Input    : char *s  : string to be searched.
  135. //+ Output   : return a pointer to the string pointing the path.
  136.  
  137. char *FindPath(char *s)
  138. {
  139.     char *pdest;
  140.     int result;
  141.     char buf[100];
  142.     
  143.     pdest = strrchr( s, '\\' );
  144.     result = pdest - s + 1;
  145.     if( pdest != NULL )
  146.     {
  147.         memcpy(buf,s,result);
  148.         buf[result]='\0';  
  149.         strupr(buf);        
  150.         return (buf);
  151.     }
  152.     else   
  153.     return NULL;
  154. }
  155.  
  156. //+ Function : int GetParameter(char *c)
  157. //+ Purpose  : get argument if entered.
  158. //+ Input    : char *s  : string to be searched.
  159. //+ Output   : return : 1 : invalid argument.
  160. //+                   : 0 : Ok.
  161.  
  162. int GetParameter(char *c)
  163. {
  164.     int prm;
  165.     if (c[0] != '-' && c[0] != '/') return 1;
  166.     if (strlen(c) != 3 && strlen(c) != 2 ) return(1); // err
  167.     if (strlen(c) <= 2 && !isdigit(c[1])) return(1);
  168.     if (strlen(c) == 3 && !isdigit(c[2])) return(1);
  169.     sscanf(c+1,"%d",&prm);    
  170.     if (prm>20) return(1);
  171.     spaceno=prm;
  172.     return (0);
  173. }
  174.  
  175. void Error()
  176. {
  177.     printf("Usage:\n");
  178.     printf("\tstruct {path}filename {-arg}\n\n");
  179.     printf("\tThe filename parameter may use DOS wildcard \n");
  180.     printf("\t\tcharacters (* and ?).\n");
  181.     printf("\targ is unit number of spaces to insert each \n");
  182.     printf("\t\tline. Default is 4 and maximum is 20 allowed.\n\n"); 
  183.     printf("\tExamples:\n");
  184.     printf("\t\tstruct c:\\myproj\\*.c\n");
  185.     printf("\t\tstruct \\myproj\\ket*.c -5\n");
  186.     printf("\t\tstruct \\myproj\\ketpcmn.c /10\n");
  187.     
  188.     exit(2);
  189. }
  190.  
  191. int main(int argc,char *argv[])
  192. {
  193.     
  194.     int kk, sts, cnt=0,
  195.     pp,   
  196.     cmt1, // comment like /* */
  197.     cmt2, // comment like //
  198.     sqm,  // single quotation mark              
  199.     dqm;  // double quotation mark                
  200.     char path[100];  
  201.     
  202.     struct _find_t  c_file;
  203.     
  204.     printf("\nStruct Version 1.0, C/C++ Structure Organizator\n");
  205.     printf("The Hekla Information Society, 1995\n"); 
  206.     printf("Ahmet Emre, CompuServe #100335,2771\n");
  207.     printf("Shareware, Feel free to distribute\n\n");
  208.     
  209.     strcpy(path,FindPath(argv[1]));
  210.     
  211.     if (argc==3 && GetParameter(argv[2]) || argc <2 ) Error();    
  212.     if (argc==2 && (argv[1][0]=='-' || argv[1][0]=='/')) Error();
  213.     
  214.     
  215.     sts=_dos_findfirst( argv[1], _A_NORMAL, &c_file );
  216.     
  217.     while(!sts)
  218.     {
  219.         strcpy(inname,path);
  220.         strcat(inname,c_file.name);    
  221.         printf("Struct: %s ",inname);
  222.         in = fopen(inname,"r");        
  223.         if (in == NULL) 
  224.         {
  225.             printf("\nCannot open input file: %s",inname);
  226.             exit(1);
  227.         }
  228.         out = fopen(outname,"w");
  229.         if (out == NULL) 
  230.         {
  231.             printf("\nCannot open output file.");
  232.             fclose(in);
  233.             exit(1);
  234.         }
  235.         colptr=0;        
  236.         cmt1=0;                 
  237.         while( fgets( buffer, MAX_LINE_SIZE, in ) != NULL)
  238.         {
  239.             ob=0; /* out buffer counter */
  240.             pp=cmt2=sqm=dqm=arabra=0;
  241.             araprev=colptr;
  242.             memset(outbuffer,'\0',MAX_LINE_SIZE);
  243.             
  244.             for (kk=0; buffer[kk] != '\0'; kk++)
  245.             {
  246.                 if (buffer[kk]=='/' && buffer[kk+1]=='*' && cmt2==0) // comment begin
  247.                 cmt1=1;
  248.                 if (buffer[kk]=='/' && buffer[kk+1]=='/') // comment 
  249.                 cmt2=1;
  250.                 if (buffer[kk]=='*' && buffer[kk+1]=='/' && cmt2==0) // comment end
  251.                 cmt1=0; 
  252.                 if (buffer[kk]=='\'') // single quotation mark
  253.                 {
  254.                     if((kk==1 && buffer[kk-1] == '\\' ) ||
  255.                     (kk>1 && buffer[kk-2] != '\\' && buffer[kk-1] == '\\')); 
  256.                     else
  257.                     if (sqm) sqm=0;
  258.                     else sqm=1;
  259.                 }
  260.                 if (buffer[kk]=='\"') // double quotation mark            
  261.                 {
  262.                     if((kk==0 && buffer[kk-1] =='\\') ||
  263.                     (kk>1 && buffer[kk-2] != '\\' && buffer[kk-1] == '\\')); 
  264.                     else
  265.                     if (dqm) dqm=0;
  266.                     else dqm=1;
  267.                 }
  268.                 if ((buffer[kk] =='\t' && ob==0) ||
  269.                 buffer[kk] =='\n' ||
  270.                 buffer[kk] =='\r' || 
  271.                 buffer[kk] =='\f' || 
  272.                 buffer[kk] == ' ' && ob==0 && cmt1==0  ) ;
  273.                 else
  274.                 {
  275.                     if (buffer[kk]=='\t') buffer[kk]=' '; // fill space instead of tab
  276.                     
  277.                     if (!(sqm || dqm || cmt1 || cmt2))
  278.                     {
  279.                         if (buffer[kk]=='{' && ob>0) 
  280.                         {
  281.                             colptr += spaceno;  
  282.                             arabra=1;             
  283.                         }
  284.                         if (buffer[kk]=='}' && ob>0) 
  285.                         {
  286.                             colptr -= spaceno;  
  287.                             if (colptr < 0) colptr=0;
  288.                             arabra=1;             
  289.                         }
  290.                     }
  291.                     
  292.                     if ((buffer[kk] != '{' && buffer[kk] != '}')
  293.                     || cmt1 || cmt2 || sqm || dqm || arabra ) 
  294.                     {
  295.                         outbuffer[ob]=buffer[kk];
  296.                         ob++;            
  297.                         pp=0;
  298.                     }
  299.                     else 
  300.                     {
  301.                         OutFileWrite(buffer[kk]);              
  302.                         memset(outbuffer,'\0',MAX_LINE_SIZE);
  303.                         ob=0;
  304.                         pp=1; // parantez islendi..
  305.                     }
  306.                 }
  307.                 
  308.             }
  309.             if (pp==0) OutFileWrite(' '); 
  310.         }
  311.         
  312.         fclose(in);
  313.         fclose(out);
  314.         if (ccopy(outname,inname)) 
  315.         {
  316.             printf("Error in copy from %s to %s",outname,inname);        
  317.         }
  318.         else cnt++;
  319.         printf("\n");
  320.         sts=_dos_findnext( &c_file );
  321.         
  322.     }
  323.     remove(outname);
  324.     
  325.     if (cnt==0) printf("\nNo source found to struct.\n");
  326.     else if (cnt==1)printf("\n1 source has been structed with %d spaces\n",spaceno);
  327.     else printf("\n%d sources has been structed with %d spaces\n",cnt,spaceno);
  328.     
  329.     return(0);                      
  330.     
  331. }
  332.  
  333.